package sum.achilles.com.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.greenrobot.event.EventBus;
import sum.achilles.com.Bean.Bean_NoteInfo;
import sum.achilles.com.Bean.Bean_Note_Item_del;
import sum.achilles.com.Bean.EventBus_Notice;
import sum.achilles.com.database.DBHelper;
import sum.achilles.com.database.NoteInfo;
import sum.achilles.com.database.UserInfo;
import sum.achilles.com.sum.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment implements View.OnClickListener {

    // Content View Elements

    private TextView mTv_history_data;
    private TextView mTv_today_data;
    private TextView mTv_empty_data;
    private TextView mTv_add_data;
    private TextView mTv_sum;

    // End Of Content View Elements

    private DBHelper dbHelper;
    private UserInfo userInfo;

    private int history_data;
    private int today_data;
    private int time_data;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        EventBus.getDefault().register(this);
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        bindViews(view);
        getDataFormDB();

        // Inflate the layout for this fragment
        return view;
    }

    private void bindViews(View rootView) {

        mTv_sum = (TextView) rootView.findViewById(R.id.tv_sum);
        mTv_history_data = (TextView) rootView.findViewById(R.id.tv_history_data);
        mTv_today_data = (TextView) rootView.findViewById(R.id.tv_today_data);
        mTv_empty_data = (TextView) rootView.findViewById(R.id.tv_empty_data);
        mTv_add_data = (TextView) rootView.findViewById(R.id.tv_add_data);

        mTv_sum.setOnClickListener(this);
        mTv_empty_data.setOnClickListener(this);
        mTv_add_data.setOnClickListener(this);
    }

    private Date getNowDayDate() {
        return new Date();
    }

    /**
     * @param oldTime 较小的时间
     * @param newTime 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)
     * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
     * @throws ParseException 转换异常
     * @author LuoB.
     */
    private boolean isToday(Date oldTime, Date newTime) {
        if (newTime == null) {
            newTime = new Date();
        }
        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(newTime);
        Date today = null;
        try {
            today = format.parse(todayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //昨天 86400000=24*60*60*1000 一天
        if ((today.getTime() - oldTime.getTime()) > 0 && (today.getTime() - oldTime.getTime()) <= 86400000) {
            return false;
        } else if ((today.getTime() - oldTime.getTime()) <= 0) { //至少是今天
            return true;
        } else { //至少是前天
            return false;
        }

    }

    private void getDataFormDB() {
        dbHelper = DBHelper.getInstance(getActivity());
        UserInfo userInfo_db = dbHelper.get_UserInfo4DB_by_first();

        if (userInfo_db == null) {
            userInfo = new UserInfo();
            history_data = 0;
            today_data = 0;

        } else {
            userInfo = userInfo_db;

            if (isToday(userInfo.getDate(), getNowDayDate())) {
                history_data = userInfo.getHistoryTime();
                today_data = userInfo.getTodayTime();
            } else {

                dbHelper.del_noteInfo_all();

                int temp_today_time = userInfo.getTodayTime();
                history_data = userInfo.getHistoryTime();

                if (temp_today_time != 0) {
                    today_data = 0;

                    userInfo.setDate(getNowDayDate());
                    userInfo.setTodayTime(today_data);

                    dbHelper.add_UserInfo2DB(userInfo);
                }
            }
        }

        time_data = 0;
        mTv_history_data.setText(String.format(getString(R.string.string_history_data), history_data + ""));
        mTv_today_data.setText(String.format(getString(R.string.string_today_data), today_data + ""));
    }


    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sum: {
                time_data++;
                mTv_sum.setText(time_data + "");
                break;
            }
            case R.id.tv_empty_data: {

                Bean_NoteInfo bean_noteInfo = new Bean_NoteInfo(dbHelper.get_NoteInfo_all());
                EventBus.getDefault().post(bean_noteInfo);
                EventBus.getDefault().post(new EventBus_Notice(EventBus_Notice.OPEN_DRAWLAYOUT));
                time_data = 0;
                mTv_sum.setText(time_data + "");
                break;
            }
            case R.id.tv_add_data: {
                if (time_data == 0) {
                    Toast.makeText(getActivity(), "数据为空！", Toast.LENGTH_SHORT).show();
                } else {
                    today_data += time_data;
                    history_data += time_data;


                    userInfo.setHistoryTime(history_data);
                    userInfo.setTodayTime(today_data);
                    userInfo.setDate(getNowDayDate());
                    dbHelper.add_UserInfo2DB(userInfo);

                    NoteInfo noteInfo = new NoteInfo(time_data, getNowDayDate());
                    dbHelper.add_NoteInfo2DB(noteInfo);

                    time_data = 0;
                    mTv_sum.setText(time_data + "");
                    mTv_history_data.setText(String.format(getString(R.string.string_history_data), history_data + ""));
                    mTv_today_data.setText(String.format(getString(R.string.string_today_data), today_data + ""));

                    Toast.makeText(getActivity(), "赞~", Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }
    }

    public void onEventMainThread(EventBus_Notice eventBus_notice) {
        if (eventBus_notice.getType() == EventBus_Notice.CLEAR_ALL_DATA) {
            userInfo.setDate(getNowDayDate());
            userInfo.setHistoryTime(0);
            userInfo.setTodayTime(0);

            dbHelper.add_UserInfo2DB(userInfo);

            history_data = today_data = time_data = 0;

            mTv_sum.setText(time_data + "");
            mTv_history_data.setText(String.format(getString(R.string.string_history_data), history_data + ""));
            mTv_today_data.setText(String.format(getString(R.string.string_today_data), today_data + ""));

            dbHelper.del_noteInfo_all();
        }
    }

    public void onEventMainThread(Bean_Note_Item_del bean_note_item_del) {
        history_data = userInfo.getHistoryTime() - bean_note_item_del.getTimes();
        today_data = userInfo.getTodayTime() - bean_note_item_del.getTimes();

        userInfo.setHistoryTime(history_data);
        userInfo.setTodayTime(today_data);

        dbHelper.add_UserInfo2DB(userInfo);

        mTv_history_data.setText(String.format(getString(R.string.string_history_data), history_data + ""));
        mTv_today_data.setText(String.format(getString(R.string.string_today_data), today_data + ""));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
